package com.controller;

import com.entity.Chart;
import com.service.ChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ChartController{

    @Autowired
    private ChartService chartService;

    public void setChartService(ChartService chartService) {
        this.chartService = chartService;
    }

    @RequestMapping(value = {"/add", "/add.html"}, method = {RequestMethod.GET})
    public ModelAndView add(HttpServletRequest request,
                            HttpServletResponse response) {
        //Map<String, Object> map = new HashMap<String, Object>();
        // Chart chart = new Chart();
        // chart.setPid("0001");
        //chart.setPname("KLOSE");
        // this.chartService.add(chart);
        return null;
        // return toView("add", map);
    }

    @RequestMapping(value = {"/add", "/add.html"}, method = {RequestMethod.POST})
    public ModelAndView addChart(HttpServletRequest request,
                                 HttpServletResponse response,
                                 @ModelAttribute("chart") Chart chart) {
        Map<String, Object> map = new HashMap<String, Object>();
        System.out.println(chart);
        map.put("message", "成功添加数据到库," + chart);
        this.chartService.add(chart);
        return null;
        //return toView("message", map);
    }

    @RequestMapping(value = {"/{pid:\\d+}/query", "/{pid:\\d+}/query.html"}, method = {
            RequestMethod.GET, RequestMethod.POST})
    public ModelAndView queryChart(HttpServletRequest request,
                                   HttpServletResponse response, @PathVariable("pid") String pid) {
//        Map<String, Object> map = new HashMap<String, Object>();
//        System.out.println(pid);
//        Chart chart = this.chartService.get(pid);
//        if (null != chart) {
//            map.put("message", "查询Id=" + pid + "的用户名为:" + chart.getPname());
//            System.out.println("查询Id=" + pid + "的用户名为:" + chart.getPname());
//        } else {
//            map.put("message", "没有查询到与Id=" + pid + "相关的数据");
//        }
        return null;
        //return toView("message", map);
    }

    @RequestMapping(value = {"chart/{typeName:\\s+}/queryCharts", "/{typeName:\\d+}/queryCharts.html"}, method = {
            RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<Chart> queryCharts(HttpServletRequest request,
                                   HttpServletResponse response, @PathVariable("typeName") String typeName) {

        List<Chart> charts = chartService.getCharts(typeName);
        return charts;
    }

    @RequestMapping(value = {"chart/queryCharts/{typeName}"}, method = {
            RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public List<Chart> queryCharts1(HttpServletRequest request,
                                   HttpServletResponse response, @PathVariable("typeName") String typeName) {

        List<Chart> charts = chartService.getCharts(typeName);
        return charts;
    }

    @RequestMapping(value = {"/{id:\\d+}/delete", "/{id:\\d+}/delete.html"}, method = {
            RequestMethod.GET, RequestMethod.POST})
    public ModelAndView deleteChart(HttpServletRequest request,
                                    HttpServletResponse response, @PathVariable("id") String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            this.chartService.delete(id);
            map.put("message", "删除Id为" + id + "的用户成功.");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message", "删除Id为" + id + "的用户失败, " + e.getMessage());
        }
        return null;
        //  return new ModelAndView("message", map));
    }

}
